package com.charley.icloud.office.realize;

import com.charley.icloud.office.enums.WordContentTypeEnum;
import com.charley.icloud.office.factory.GenerateWordFactory;
import com.charley.icloud.office.model.domain.LabelData;
import com.charley.icloud.office.model.domain.PictureContentData;
import com.charley.icloud.office.service.IGenerateWord;
import com.deepoove.poi.data.PictureRenderData;
import com.deepoove.poi.util.BytePictureUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
 * 根据图片实体中是否有图片链接来创建数据
 * */
@Component
public class PictureGenerateWord implements IGenerateWord {

    @PostConstruct
    private void init(){
        GenerateWordFactory.register(WordContentTypeEnum.PICTURE,this);
    }

    @Override
    public Object generateWord(LabelData data) {
        PictureContentData picture = (PictureContentData) data;
        return StringUtils.isNotBlank(picture.getPicUrl()) ?
                new PictureRenderData(picture.getWidth(),picture.getHeight(),picture.getPicType().getPicName(), BytePictureUtils.getUrlBufferedImage(picture.getPicUrl()))
                : new PictureRenderData(picture.getWidth(),picture.getHeight(),picture.getFile());
    }
}
